import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Main from "@/views/Main";
import Dept from "@/views/sys/Dept";
import User from "@/views/sys/User";
import Role from "@/views/sys/Role";
import Menu from "@/views/sys/Menu";
import Post from "@/views/sys/Post";
import Course from "@/views/tb/Course";
import Activity from "@/views/tb/Activity";
import axios from "axios"; 
import store from "@/store/index"

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "Login",
    component: Login,
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/main",
    name: "Main",
    component: Main,
    children:[
      {
        path: "/dept/list",
        name: "Dept",
        component: Dept,
      },
      {
        path: "/user/list",
        name: "User",
        component: User,
      },
      {
        path: "/role/list",
        name: "Role",
        component: Role,
      },
      {
        path: "/menu/list",
        name: "Menu",
        component: Menu,
      },
      {
        path: "/post/list",
        name: "Post",
        component: Post,
      },
      {
        path: "/activity/list",
        name: "Activity",
        component: Activity,
      },
      {
        path: "/course/list",
        name: "Course",
        component: Course,
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
//全局路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path=="/login" || to.path=="/"){
    //放行
    next();
  }else{
    //判断是否登录
    let token=store.state.token;
    if(token){
      if(store.state.userInfo.username=='未登录'){
        axios.get("/api/getInfo")
          .then((res) => {
            if (res.data.code == 200) {
              store.commit("setUserInfo",{
                "username":res.data.username,
                "avatar":res.data.avatar,
                "permissions":res.data.permissions
              });
            } else {
              this.$message.error(res.data.msg);
            }
          })
        .catch((err) => {
          console.log(err);
        });
      }
      next();
    }else{
      Vue.prototype.$message.error("无权访问，请先登录");
      next("/login");
    }
    // //判断是否登录
    // let token=store.state.token;
    // if(token){
    //   //判断有没有权限
    //   if(store.state.permissions || store.state.permissions.length == 0){
    //     axios.get("/api/getInfo").then((res)=>{
    //       if(res.data.code==200){
    //         store.commit("setUsername",res.data.username);
    //         store.commit("setPermissions",res.data.permissions);
    //         store.commit("setRoutes",res.data.menus);
    //         console.log("formatRouters(res.data.menus)",formatRouters(res.data.menus))
    //         router.addRoute(formatRouters(res.data.menus));
    //       }
    //     }).catch((err)=>{
    //       console.log(err);
    //     });
    //   }

    //   next();
    // }else{
    //   Vue.prototype.$message.error("未登录，请先登录");
    //   next("/login");
    // }
  }
})

// // formatRouters()方法用于对响应过来的菜单数据(data)进行格式化
// const formatRouters = (menus) => {
//   // menus为后端响应过来的List<Menu>菜单列表数据
//   const fmtRouters = [] // 定义一个空的路由对象数组
//   // 对从后端响应过来的List<Menu>菜单列表数据使用forEach进行循环遍历
//   menus.forEach(router => {
//     // 循环出的每个Menu菜单对象, 从每个菜单对象中取出个别值再赋值给router
//     const { path, component, menuName, icon } = router
//     let { children } = router
//     if (children && children instanceof Array ) {
//       // 如果children存在(即当前Menu菜单对象下有子菜单对象)，并且它是Array数组类型
//       // 使用递归取出当前菜单里的子菜单对象信息
//       children = formatRouters(children)
//     }
//     // 单独对router路由对象中的属性格式化后，挨个赋值给fmtRouter对象
//     const fmtRouter = {
//       path: path,
//       name: menuName,
//       meta: { icon: icon, title: menuName },
     
//      // component的值在router/index.js路由文件中是一个组件对象，而在t_menu菜单表中component是字符串类型的值
//      // 所以我们这里需要将这个字符串类型的值转换成具体某个组件对象，所有自定义组件将创建在views目录中
//       // component: component === require('@/views/' + component + '.vue').default
//     }
//     if(children && children.length){
//       fmtRouter.children=children
//     }
//     if(component){
//       fmtRouter.component=require('@/views/' + component + '.vue').default
//     }
//     // 再将格式化好的fmtRouter路由对象添加到fmtRouters路由对象数组中
//     fmtRouters.push(fmtRouter)
//   })
//   // 将查询到的菜单列表(路由对象数组)进行返回
//   return fmtRouters
// }

export default router
